package cn.smbms.dao;

import cn.smbms.dao.user.UserMapper;
import cn.smbms.utils.MyBatiesUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import javax.swing.plaf.multi.MultiButtonUI;
import java.io.IOException;
import java.io.InputStream;

public class UserDao {
    private Logger logger = Logger.getLogger(UserDao.class);

    @Test
    public void userCount() {

        try {
            // 1 读取配置文件
            InputStream is = Resources.getResourceAsStream("configuration.xml");
            // 2 把配置文件给框架，
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            // 3 创建连接 ,查询
            SqlSession sqlSession = factory.openSession();
            int count = sqlSession.selectOne("cn.smbms.user.UserMapper.count");
            System.out.println(count);
            logger.debug("数据库中的用户有：" + count);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void test01() {
        SqlSession session = MyBatiesUtil.createSession();
        //int count  = session.selectOne("cn.smbms.dao.user.UserMapper.count");
        int count = session.getMapper(UserMapper.class).count();
        logger.debug("工具类访问的用户数" + count);
        session.close();
    }

}


